<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="【Linux】LED驱动, ZhengN_Blog">
    <meta name="description" content="前言上一篇我们分享了字符设备驱动框架：嵌入式Linux驱动基础，当时分享的是hello驱动程序。学STM32我们从点灯开始，学Linux驱动我们自然也要点个灯来玩玩，尽量在从这些基础例程中榨取知识，细抠、细抠，为之后更复杂的知识打好基础。
">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>【Linux】LED驱动 | ZhengN_Blog</title>
    <link rel="icon" type="image/jpeg" href="/favicon.jpg">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">ZhengN_Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">ZhengN_Blog</div>
        <div class="logo-desc">
            
            printf(&#34;每天进步一点点~&#34;);
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/zhengnianli/stm32_weather" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/zhengnianli/stm32_weather" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/5.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        【Linux】LED驱动
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Linux/" target="_blank">
                                <span class="chip bg-color">Linux</span>
                            </a>
                        
                            <a href="/tags/驱动/" target="_blank">
                                <span class="chip bg-color">驱动</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Linux/" class="post-category" target="_blank">
                                Linux
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-03-12
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>文章字数:&nbsp;&nbsp;
                        2k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>阅读时长:&nbsp;&nbsp;
                        7 分
                    </div>
                    
                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>上一篇我们分享了字符设备驱动框架：<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003112&idx=1&sn=2e930bff06f28d838e37586226b6c46a&chksm=7e0d63ef497aeaf99c120b80b42c907a7e1494e17bc5c101d91c097813024b7fcc517578f03f#rd" target="_blank" rel="noopener">嵌入式Linux驱动基础</a>，当时分享的是hello驱动程序。学STM32我们从点灯开始，学Linux驱动我们自然也要点个灯来玩玩，尽量在从这些基础例程中榨取知识，细抠、细抠，为之后更复杂的知识打好基础。</p>
<h2 id="与硬件无关的LED驱动"><a href="#与硬件无关的LED驱动" class="headerlink" title="与硬件无关的LED驱动"></a>与硬件无关的LED驱动</h2><p>回顾hello驱动程序，我们的根据实际需求对其进行写字符串与读字符串操作。这里我们当然也要根据实际来思考我们的LED驱动程序。在STM32点灯的时候，一般输出低电平点灯，输出高电平灭灯。在嵌入Linux操作系统的情况下，我们自然也要想到有个写1/0的思想。类比我们上一篇的hello程序：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkhvT.png" alt></p>
<p>我们的LED程序自然要写入的数据为0/1来点亮、熄灭LED。这里我们做的实验室与硬件无关的LED实验：我们的驱动程序在收到应用程序发送过来的0时打印<code>led on</code>、收到1时打印<code>led off</code>。模仿上一篇的hello程序，我们修改得到的与硬件无关的LED程序（核心部分）如下：</p>
<p><strong>LED应用程序：</strong></p>
<p><img src="https://s1.ax1x.com/2020/03/13/8Mk6Ej.png" alt></p>
<p><strong>LED驱动程序：</strong></p>
<p><img src="https://s1.ax1x.com/2020/03/13/8Mkg5n.png" alt></p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkRCq.png" alt></p>
<p>加载led驱动模块及运行应用程序：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkW80.png" alt></p>
<h2 id="与硬件有关的LED驱动"><a href="#与硬件有关的LED驱动" class="headerlink" title="与硬件有关的LED驱动"></a>与硬件有关的LED驱动</h2><p>上面那一节分享的是与硬件无关的LED驱动实验，主要是为了理清LED驱动的大体思路。这里我们再加入与硬件有关的相关操作以构造与硬件有关的LED驱动程序。</p>
<p>我们在进行STM32的裸机编程的时候，对一些外设进行配置其实就是操作一些地址的过程，这些外设地址在芯片手册中可以看到：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8Mk5KU.png" alt></p>
<p>这是地址映射图，这里图中只是列出的外设的<code>边界地址</code>，每个外设又有很多寄存器，这些寄存器的地址都是对外设基地址进行偏移得到的。同样的，对于NXP的IMX6ULL芯片来说，也是有类似这样的地址的：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkIrF.png" alt></p>
<p>此时我们要编写Linux系统下的led驱动，涉及到硬件操作的地方操作的并不是这些地址（物理地址），而是操作系统给我们提供的地址（虚拟地址）。操作系统根据物理地址来给我们生成一个虚拟地址，我们的led驱动操控这个地址就是间接的操控物理地址。至于这两个地址是怎么联系起来的，里面个原理我们暂且不展开。我们从函数层面来看，内核给我们提供了ioremap 函数，这个函数可以把物理地址映射为虚拟地址。这个函数在内核文件<code>arch/arm/include/asm/io.h</code>  中：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">void</span> __iomem <span class="token operator">*</span><span class="token function">ioremap</span><span class="token punctuation">(</span>resource_size_t res_cookie<span class="token punctuation">,</span> size_t size<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<ul>
<li>res_cookie：要映射给的物理起始地址 。</li>
<li>size：要映射的内存空间大小。 </li>
<li>返回值： 指向映射后的虚拟空间首地址。 </li>
</ul>
<p>与<code>ioremap</code>函数相对应的函数为：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">iounmap</span> <span class="token punctuation">(</span><span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span></code></pre>
<ul>
<li>addr：要取消映射的虚拟地址空间首地址。</li>
</ul>
<p>地址映射完成之后，我们可以直接通过指针来访问虚拟地址，如：</p>
<pre class=" language-c"><code class="language-c"><span class="token operator">*</span>GPIO5_DR <span class="token operator">&amp;</span><span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">/* GPIO5_IO03输出低电平 */</span>
<span class="token operator">*</span>GPIO5_DR <span class="token operator">|</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">/* GPIO5_IO03输出高电平 */</span></code></pre>
<p>这里简单介绍一下<code>i.MX 6ULL</code>的GPIO。对于<code>i.MX 6ULL</code>来说，以数字来给IO端口（组别）命令，GPIO5为第五组，所以GPIO5_IO03为第五组端口的第3个引脚。而STM32中是以大写字母来表示端口（组别），如PA3表示A端口的第3个引脚。</p>
<p><code>i.MX 6ULL</code>有 5 组 GPIO（GPIO1～ GPIO5），每组引脚最多有 32 个：</p>
<pre class=" language-c"><code class="language-c">GPIO1 有 <span class="token number">32</span> 个引脚： GPIO1_IO0<span class="token operator">~</span>GPIO1_IO31；
GPIO2 有 <span class="token number">22</span> 个引脚： GPIO2_IO0<span class="token operator">~</span>GPIO2_IO21；
GPIO3 有 <span class="token number">29</span> 个引脚： GPIO3_IO0<span class="token operator">~</span>GPIO3_IO28；
GPIO4 有 <span class="token number">29</span> 个引脚： GPIO4_IO0<span class="token operator">~</span>GPIO4_IO28；
GPIO5 有 <span class="token number">12</span> 个引脚： GPIO5_IO0<span class="token operator">~</span>GPIO5_IO11；</code></pre>
<p>地址映射完成之后，我们不仅可以通过指针来访问虚拟地址，而且还可以使用内核给我们提供的一些读写函数：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 写操作函数 */</span>
<span class="token keyword">void</span> <span class="token function">writeb</span><span class="token punctuation">(</span>u8 value<span class="token punctuation">,</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">writew</span><span class="token punctuation">(</span>u16 value<span class="token punctuation">,</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">writel</span><span class="token punctuation">(</span>u32 value<span class="token punctuation">,</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/* 读操作函数 */</span>
u8 <span class="token function">readb</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
u16 <span class="token function">readw</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
u32 <span class="token function">readl</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">volatile</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>addr<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>writeb、 writew 和 writel 这三个函数分别对应 8bit、 16bit 和 32bit 写操作，参数 value 是要写入的数值， addr 是要写入的地址。 </p>
<p>readb、 readw 和 readl 这三个函数分别对应 8bit、 16bit 和 32bit 读操作，参数 addr 就是要读取写内存地址，返回值就是读取到的数据。</p>
<p>此时我们可以把上一节的<code>led_init</code>函数<code>led_drv_write</code>函数进行修改：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8Mk7VJ.png" alt></p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkHa9.png" alt></p>
<p>与STM32一样，对于<code>i.MX 6ULL</code>的GPIO外设来说，也有很多寄存器：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8Mkb5R.png" alt></p>
<p>上面我们只是点一个灯，如果是要点多个灯呢？那就得操控多个GPIO。如果进行地址映射的写法还像上面那样，代码就会显得很臃肿。回想一下我们STM32，GPIO外设通过结构体来管理它的寄存器：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkO8x.png" alt></p>
<p>这里的__IO是个宏，代表C语言的关键字<code>volatile</code> ，为了防止编译器对我们的一些硬件操作进行优化，从而得不到想要的结果。比如：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 假设REG为寄存器的地址 */</span>
uint32 <span class="token operator">*</span>REG<span class="token punctuation">;</span>
<span class="token operator">*</span>REG <span class="token operator">=</span> <span class="token number">0</span>；  <span class="token comment" spellcheck="true">/* 点灯 */</span>
<span class="token operator">*</span>REG <span class="token operator">=</span> <span class="token number">1</span>；  <span class="token comment" spellcheck="true">/* 灭灯 */</span></code></pre>
<p>此时若是REG不加<code>volatile</code>进行修饰，则点灯操作将被优化掉，只执行灭灯操作。</p>
<p>在这里，我们也可以模仿STM32那样子，用一个结构体来对<code>i.MX 6ULL</code>的GPIO的寄存器进行管理，如：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">struct</span> GPIO_RegDef
<span class="token punctuation">{</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> DR<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> GDIR<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> PSR<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ICR1<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ICR2<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> IMR<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> ISR<span class="token punctuation">;</span>
  <span class="token keyword">volatile</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> EDGE_SEL<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre>
<p>结构体里的成员排序是要按照特定顺序来的：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkX26.png" alt></p>
<p>因为这些寄存器都是相对于GPIO外设的基地址作偏移得到的，比如：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkjxK.png" alt></p>
<p>不能打乱顺序，否则就不能正确访问到对应的寄存器了。用结构体进行管理之后，我们就可以用类似下面的方式进行映射：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">struct</span> GPIO_RegDef <span class="token operator">*</span>GPIO5 <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span><span class="token number">0x20AC000</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> GPIO_RegDef<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>然后就可以向STM32那样来操控GPIO寄存器，如：</p>
<pre class=" language-c"><code class="language-c">GPIO5<span class="token operator">-></span>DR <span class="token operator">&amp;</span><span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">/* GPIO5_IO03输出低电平 */</span>
GPIO5<span class="token operator">-></span>DR <span class="token operator">|</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">/* GPIO5_IO03输出高电平 */</span></code></pre>
<h2 id="与硬件有关的LED驱动（升级版）"><a href="#与硬件有关的LED驱动（升级版）" class="headerlink" title="与硬件有关的LED驱动（升级版）"></a>与硬件有关的LED驱动（升级版）</h2><p>上一节我们分享的LED驱动是一个常规的LED驱动，只能适用于我们当前的开发版，所以是一个专用的LED驱动程序。若是换了另一块板，led所连接的gpio引脚可能不一样了，我们就修改我们的驱动程序<code>led_drv.c</code>里与寄存器相关的操作。有没有更好的办法不用再修改我们的<code>led_drv.c</code>驱动程序了？</p>
<p>若是<code>led_drv.c</code>不用再修改了，那么这个<code>led_drv.c</code>驱动就是一个通用的驱动程序了。具体可查看<code>韦东山老师的《嵌入式Linux应用开发完全手册第2版》第五篇第3~7节进行学习</code>。</p>
<p>下面来简单地梳理一下：</p>
<p><img src="https://s1.ax1x.com/2020/03/13/8MkxKO.png" alt></p>
<p>由于篇幅问题，具体的部分就不贴出来了。</p>
<p>之前的笔记中：<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100002865&idx=1&sn=ebdcc4662703bf9cef6ec85c50cbd7b5&chksm=7e0d62f6497aebe03c13c23eb9663fecaa23ed81fc5e83edc00ab2dec4a22c52ec7953ac5820#rd" target="_blank" rel="noopener">C语言、嵌入式重点知识：回调函数</a>中我也有提到<code>通用</code>与<code>专用</code>的含义，可以了解了解加深对这两个词的认识。</p>
<p>这里我们学到了很重要的思想软件分层的思想及技巧，但也只是点了一下，未来的路还很长，需要持续学习，继续提高。</p>
<p>以上就是本次的分享，如有错误，欢迎指出！谢谢</p>
<p>参考/学习资料：</p>
<ul>
<li>百问网《嵌入式Linux应用开发完全手册第2版》</li>
<li>正点原子《I.MX6U嵌入式Linux驱动开发指南V1.2》</li>
<li>野火《i.MX Linux开发实战指南》</li>
</ul>
<hr>
<p>我的个人博客：<a href="https://www.lizhengnian.cn/">https://www.lizhengnian.cn/</a></p>
<p>我的微信公众号：嵌入式大杂烩</p>
<p>我的CSDN博客：<a href="https://blog.csdn.net/zhengnianli" target="_blank" rel="noopener">https://blog.csdn.net/zhengnianli</a></p>
<p><img src="https://s2.ax1x.com/2019/06/11/VcSFJJ.md.png" alt></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">欢迎关注ZhengN的微信公众号</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <!--<ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微信公众号</a></li>
                    </ul>-->
					<!--
                    <div id="alipay">
                        <img src="" class="reward-img" alt="支付宝打赏二维码">
                    </div>
					-->
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信公众号二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            


        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments input[type=text],
    #vcomments input[type=email],
    #vcomments input[type=url],
    #vcomments textarea {
        box-sizing: border-box;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #42b983;
        font-weight: 500;
        text-decoration: underline;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div id="vcomments" class="card-content"></div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'Vc276uuqkd8y8heuqzShe9p1-gzGzoHsz',
        appKey: 'bUwy6lANpgeVdpcQamMtC8QD',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '快来快活呀！^_^'
    });
</script>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/03/14/c-yu-yan-zhi-zhen-zhi-shi-da-hui-zong/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/10.jpg" class="responsive-img" alt="【C语言】指针知识大汇总">
                        
                        <span class="card-title">【C语言】指针知识大汇总</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            指针在C语言中是一块很重要的内容，也是比较难理解的一块内容，我们需要反复理解反复巩固才可以对其有所了解。之前也分享过指针相关的笔记，但是都比较杂，本篇笔记汇总一下指针相关的内容，包含了挺多指针相关的基础知识点。这篇笔记有点长，可以收藏下来慢
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2020-03-14
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/C语言/" class="post-category" target="_blank">
                                    C语言
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/C语言/" target="_blank">
                        <span class="chip bg-color">C语言</span>
                    </a>
                    
                    <a href="/tags/嵌入式/" target="_blank">
                        <span class="chip bg-color">嵌入式</span>
                    </a>
                    
                    <a href="/tags/位操作/" target="_blank">
                        <span class="chip bg-color">位操作</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/03/12/linux-linux-qu-dong-ji-chu-pian/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/20.jpg" class="responsive-img" alt="【Linux】Linux驱动基础">
                        
                        <span class="card-title">【Linux】Linux驱动基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            上一篇分享的：从单片机工程师的角度看嵌入式Linux中有简单提到Linux的三大类驱动：

我们学习编程的时候都会从hello程序开始。同样的，学习Linux驱动我们也从最简单的hello驱动学起。
驱动层和应用层还记得实习那会儿我第一次接
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2020-03-12
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Linux/" class="post-category" target="_blank">
                                    Linux
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/嵌入式/" target="_blank">
                        <span class="chip bg-color">嵌入式</span>
                    </a>
                    
                    <a href="/tags/Linux/" target="_blank">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由&copy;<a href="https://zhengnianli.github.io/" target="_blank">ZhengNian</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题搭建.

            
                &nbsp;<i class="fa fa-area-chart"></i>&nbsp;站点总字数:&nbsp;
                <span class="white-color">187.2k</span>
            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/zhengnianli" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>



    <a href="mailto:2238184039@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2238184039" class="tooltipped" data-tooltip="QQ联系我: 2238184039" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->



    <script src="/libs/others/clicklove.js"></script>


    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>